package com.dyrj.pig.service.impl;

import com.dyrj.pig.bean.TOrder;
import com.dyrj.pig.bean.TSuborder;
import com.dyrj.pig.bean.TSuborderExample;
import com.dyrj.pig.dao.TOrderMapper;
import com.dyrj.pig.dao.TSuborderMapper;
import com.dyrj.pig.service.ITSuborderService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 子订单 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-03-13
 */
@Service
@Log4j2
public class TSuborderServiceImpl implements ITSuborderService {

    @Resource
    private TOrderMapper tOrderMapper;
    @Resource
    private TSuborderMapper mapper;

    @Resource
    private UploadProperties uploadProperties;

    @Override
    public PageResultMsg table(TSuborder record) {
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
//        record.setUserid(loginUser.getAccount());
        PageInfo<TSuborder> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.selectList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }


    @Override
    public ResultMsg add(TSuborder record) {
        String msg = "";
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
        record.setId(UUIDUtil.uuid());
//        record.setInsertime(new Date());
//        record.setUpdatetime(new Date());
//        record.setUserid(loginUser.getAccount());
        try {
            mapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
    }


    @Override
    public TSuborder info(String id) {
        TSuborder ylMmdagl = mapper.selectByPrimaryKey(id);
        return ylMmdagl;
    }

    public ResultMsg update(TSuborder record) {
        String msg = "";
        SysUser loginUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysFrameConst.SYSTEM_USER);
        try {
            TSuborderExample tSuborderExample = new TSuborderExample();
            TSuborderExample.Criteria criteria = tSuborderExample.createCriteria();
            criteria.andDdbmEqualTo(record.getDdbm());
            List<TSuborder> listTSuborder = mapper.selectByExample(tSuborderExample);

            TOrder tOrder_p = new TOrder();
            tOrder_p.setDdbm(record.getDdbm());

            tOrder_p.setAid(new Integer(record.getAid()));
            tOrder_p.setAsj(record.getAsj());
            tOrder_p.setAxm(record.getAxm());
            tOrder_p.setAzzh(record.getAzzh());

            tOrder_p.setBid(new Integer(record.getBid()));
            tOrder_p.setBsj(record.getBsj());
            tOrder_p.setBxm(record.getBxm());
            tOrder_p.setBzzh(record.getBzzh());

            tOrder_p.setCid(new Integer(record.getCid()));
            tOrder_p.setCsj(record.getCsj());
            tOrder_p.setCxm(record.getCxm());
            tOrder_p.setCzzh(record.getCzzh());

            tOrder_p.setDid(new Integer(record.getDid()));
            tOrder_p.setDsj(record.getDsj());
            tOrder_p.setDxm(record.getDxm());
            tOrder_p.setDzzh(record.getDzzh());
            tOrderMapper.updateByPrimaryKeySelective(tOrder_p);

            if(listTSuborder.size()>0){
                for (TSuborder tsuborder:listTSuborder) {
                    if("BA".equals(tsuborder.getDdlx())){
                        tsuborder.setFkfid(new Integer(record.getBid()));
                        tsuborder.setSkfid(new Integer(record.getAid()));
                        tsuborder.setYfje(new BigDecimal(record.getBa_yfje()));
                        tsuborder.setYfddzt("未付");
                        tsuborder.setWfje(new BigDecimal(record.getBa_wfje()));
                        tsuborder.setWfddzt("未付");
                        tsuborder.setUpdatetime(new Date());
                        tsuborder.setUserid(loginUser.getId().intValue());
                    }

                    if("CB".equals(tsuborder.getDdlx())){
                        tsuborder.setFkfid(new Integer(record.getCid()));
                        tsuborder.setSkfid(new Integer(record.getBid()));
                        tsuborder.setYfje(new BigDecimal(record.getCb_yfje()));
                        tsuborder.setYfddzt("未付");
                        tsuborder.setWfje(new BigDecimal(record.getCb_wfje()));
                        tsuborder.setWfddzt("未付");
                        tsuborder.setUpdatetime(new Date());
                        tsuborder.setUserid(loginUser.getId().intValue());
                    }

                    if("DC".equals(tsuborder.getDdlx())){
                        tsuborder.setFkfid(new Integer(record.getDid()));
                        tsuborder.setSkfid(new Integer(record.getCid()));
                        tsuborder.setYfje(new BigDecimal(record.getDc_yfje()));
                        tsuborder.setYfddzt("未付");
                        tsuborder.setWfje(new BigDecimal(record.getDc_wfje()));
                        tsuborder.setWfddzt("未付");
                        tsuborder.setUpdatetime(new Date());
                        tsuborder.setUserid(loginUser.getId().intValue());
                    }

                    mapper.updateByPrimaryKeySelective(tsuborder);
                }

            }else{
                //新增逻辑
                TSuborder record_1 = new TSuborder();
                record_1.setId(UUIDUtil.uuid());
                record_1.setDdbm(record.getDdbm());
                record_1.setDdlx("BA");
                record_1.setFkfid(new Integer(record.getBid()));
                record_1.setSkfid(new Integer(record.getAid()));
                record_1.setYfje(new BigDecimal(record.getBa_yfje()));
                record_1.setYfddzt("未付");
                record_1.setWfje(new BigDecimal(record.getBa_wfje()));
                record_1.setWfddzt("未付");
                record_1.setInserttime(new Date());
                record_1.setUpdatetime(new Date());
                record_1.setUserid(loginUser.getId().intValue());

                TSuborder record_2 = new TSuborder();
                record_2.setId(UUIDUtil.uuid());
                record_2.setDdbm(record.getDdbm());
                record_2.setDdlx("CB");
                record_2.setFkfid(new Integer(record.getCid()));
                record_2.setSkfid(new Integer(record.getBid()));
                record_2.setYfje(new BigDecimal(record.getCb_yfje()));
                record_2.setYfddzt("未付");
                record_2.setWfje(new BigDecimal(record.getCb_wfje()));
                record_2.setWfddzt("未付");
                record_2.setInserttime(new Date());
                record_2.setUpdatetime(new Date());
                record_2.setUserid(loginUser.getId().intValue());

                TSuborder record_3 = new TSuborder();
                record_3.setId(UUIDUtil.uuid());
                record_3.setDdbm(record.getDdbm());
                record_3.setDdlx("DC");
                record_3.setFkfid(new Integer(record.getDid()));
                record_3.setSkfid(new Integer(record.getCid()));
                record_3.setYfje(new BigDecimal(record.getDc_yfje()));
                record_3.setYfddzt("未付");
                record_3.setWfje(new BigDecimal(record.getDc_wfje()));
                record_3.setWfddzt("未付");
                record_3.setInserttime(new Date());
                record_3.setUpdatetime(new Date());
                record_3.setUserid(loginUser.getId().intValue());

                mapper.insertSelective(record_1);
                mapper.insertSelective(record_2);
                mapper.insertSelective(record_3);

//                tOrderMapper.updateByPrimaryKey();
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
    }

    @Override
    public ResultMsg delete(String id) {
        mapper.deleteByPrimaryKey(id);
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
    }

}
